//
//  VerificationnumberViewController.m
//  Molian
//
//  Created by 胡津 on 15/4/15.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "VerificationnumberViewController.h"
#import "MLLoginTextField.h"
#include "InitDataViewController.h"
@interface VerificationnumberViewController ()
@property (strong,nonatomic)MLLoginTextField *CAPTCHAnumber;
@property (strong,nonatomic)BFPaperButton *again;

@property NSInteger countdownTime;
@property (strong, nonatomic) NSTimer *updateCodeTimer;

@end

@implementation VerificationnumberViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *rightItem  = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(rightItem)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    //第一次输入密码
    self.CAPTCHAnumber = [[MLLoginTextField alloc]initWithFrame:CGRectMake(0, 80, DeviceWidth, 40)];
    self.CAPTCHAnumber.placeholder = @"请输入验证码";
    self.CAPTCHAnumber.leftPrompt = @"   验证码    ";
    self.CAPTCHAnumber.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.CAPTCHAnumber];
    self.CAPTCHAnumber.toBeNumber = 6;
    [self.CAPTCHAnumber style];
    [self.CAPTCHAnumber becomeFirstResponder];
    
    //获取验证码按钮
    self.again = [[BFPaperButton alloc]initWithFrame:CGRectMake(DeviceWidth-110, 80, 100, 30)raised:NO];
    [self.again setTitle:@"重新获取" forState:UIControlStateNormal];
    [self.again setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
    self.again.layer.masksToBounds = YES;//开启圆角
    [self.again.layer setCornerRadius:15];
    [self.again addTarget:self action:@selector(getVirifyCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.again.layer setBorderColor:APPCOLOR(1) .CGColor];
    [self.again.layer setBorderWidth:1];
    self.again.titleLabel.adjustsFontSizeToFitWidth = YES;
    self.again.titleLabel.font = [UIFont fontWithName:FontName size:14];
    [self.view addSubview:self.again];
    
    //重新获取不可点击
    [self.again setEnabled:NO];
    
    //获取验证码时间
    self.countdownTime = 30;
    self.updateCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountdownTime) userInfo:nil repeats:YES];
    
    //密码提示
    UILabel *prompt = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, DeviceWidth, 20)];
    prompt.text = @"验证码有效期为10分钟";
    prompt.textColor = [UIColor grayColor];
    prompt.textAlignment = NSTextAlignmentCenter;
    prompt.font = [UIFont fontWithName:FontName size:13];
    [self.view addSubview:prompt];
    
}

//确定注册
-(void)rightItem
{
    NSString *verifyCode = self.CAPTCHAnumber.text;
    if ([verifyCode isEqualToString:@""]) {
        [HUD Prompt:@"请输入收到的验证码"];
        return;
    }
    [MLUser verifyMobilePhone:verifyCode withBlock:^(BOOL succeeded, NSError *error) {
        // 处理校验验证码后的返回值
        if (!error) {
            [Utils saveToDefaults:self.phoneNumber forKey:@"accountName"];
            InitDataViewController *initDataView = [[InitDataViewController alloc]init];
            [initDataView.navigationItem setHidesBackButton:YES animated:YES];
            [self.navigationController pushViewController:initDataView animated:YES];
        }
        else {
            NSString *msg = @"验证失败,请检查验证码是否正确";
            if (error.code == AVOS_RES_CODE_SMS_CODE_INVALID) {
                msg = @"验证码错误";
            }
            [HUD Prompt:msg ];
            
        }
    }];
}


// 获取验证码
- (void)getVirifyCode:(BFPaperButton *)sender {
    [MLUser requestMobilePhoneVerify:self.phoneNumber withBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [HUD hideFromViewAnimated:YES];
            [self.CAPTCHAnumber becomeFirstResponder];
            // 获取验证码成功,禁用按钮
            [self.again setEnabled:NO];
            self.countdownTime = 30;
            [self.again setTitle:@"获取验证码" forState:UIControlStateNormal];
            // 启动定时器
            self.updateCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountdownTime) userInfo:nil repeats:YES];
        } else {
            NSString *msg = @"获取失败,请重试";
            if (AVOS_RES_CODE_PHONE_NOT_EXIST == error.code) {
                msg = @"手机号码不存在";
            }
            [HUD Prompt:msg ];
        }
    }];
    [HUD setOnViewWithTitle:nil animated:YES];

}

// 修改获取验证码的倒计时
- (void)updateCountdownTime {
    self.countdownTime -=  1;
    NSString *time = [NSString stringWithFormat:@"%lds", (long)self.countdownTime];
    [self.again setTitle:time forState:UIControlStateNormal];
    // 处理倒计时结束
    if (self.countdownTime == 0) {
        [self.again setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.again setEnabled:YES];
        [self.again setBackgroundColor:[UIColor whiteColor]];
        [self.updateCodeTimer invalidate];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
